<?xml version="1.0" ?>

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
						xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd"
                  version="1.0">

   <xsl:output method="text"/>

	<xsl:include href="../../common.xsl" />
	<xsl:include href="../../convert.xsl" />
   
   <xsl:template match="lzc:project"><xsl:apply-templates select="//lzc:collection[ ( @compiling ) ]" /></xsl:template>

   <xsl:template match="lzc:collection">
         <xsl:variable name="v_DetailName">Derived.<xsl:value-of select="@object" /><xsl:text>Detail</xsl:text>
         </xsl:variable>
         <xsl:variable name="v_CollectionName">
            <xsl:value-of select="@object" /><xsl:text>Collection</xsl:text>
         </xsl:variable>
         <xsl:variable name="v_NamespacePrefix">
				<xsl:if test="/lzc:project/@namespaceprefix">
					<xsl:value-of select="/lzc:project/@namespaceprefix" />
					<xsl:text>:</xsl:text>
				</xsl:if>
         </xsl:variable>
         <xsl:variable name="v_RuntimeNamespace">
				<xsl:if test="ancestor::lzc:project/@defaultnamespace">
					<xsl:value-of select="ancestor::lzc:project/@defaultnamespace"/>
				</xsl:if>
         </xsl:variable>
         <xsl:variable name="v_BusinessConceptNamespace">
				<xsl:if test="/lzc:project/@businessconcept">
					<xsl:text>.</xsl:text>
					<xsl:value-of select="/lzc:project/@businessconcept" />
				</xsl:if>
         </xsl:variable>
<xsl:text>
using System;
using System.Collections;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = </xsl:text>
	<xsl:value-of select="$v_RuntimeNamespace" />
	<xsl:value-of select="$v_BusinessConceptNamespace" />
	<xsl:text>;

</xsl:text>

<xsl:text>namespace </xsl:text>
	<xsl:value-of select="$v_RuntimeNamespace" />
	<xsl:value-of select="$v_BusinessConceptNamespace" />
	<xsl:text>.Lazarus
{
   public class </xsl:text><xsl:value-of select="$v_CollectionName" /> : EntityCollectionBase
   {
		public virtual void Add( <xsl:value-of select="$v_DetailName" /> pSource )
		{
			this.List.Add( pSource );

			if ( pSource.ParentCollection == null )
			{
				pSource.ParentCollection	= ( Derived.<xsl:value-of select="$v_CollectionName" /> )this;
			}
			
			if ( pSource.ParentEntity == null )
			{
				pSource.ParentEntity			= this.ParentEntity;
			}
		}

		public virtual void AddAt( Int32 pIndex, <xsl:value-of select="$v_DetailName" /> pSource )
		{
			this.List.Insert( pIndex, pSource );

			pSource.ParentCollection	= ( Derived.<xsl:value-of select="$v_CollectionName" /> )this;
			pSource.ParentEntity			= this.ParentEntity;
		}
		
		public virtual void AddRange( <xsl:value-of select="$v_DetailName" />[] pSource )
		{
			foreach ( <xsl:value-of select="$v_DetailName" /> vSource in pSource )
			{
				this.Add( vSource );
			}
		}
		
		public new <xsl:value-of select="$v_DetailName" /> this[ int p_iIndex ]
		{
			get
			{
				return ( <xsl:value-of select="$v_DetailName" /> )base[ p_iIndex ];
			}
		}
		<xsl:variable name="OBJECT_REF">
			<xsl:value-of select="@object" />
		</xsl:variable>
		<xsl:variable name="TITLE_FIELD">
			<xsl:value-of select="ancestor::lzc:objects/lzc:object[ @name = $OBJECT_REF ]/@title-field" />
		</xsl:variable>
		<xsl:if test="string-length( $TITLE_FIELD ) &gt; 0">
		public <xsl:value-of select="$v_DetailName" /> this[ string p<xsl:value-of select="$TITLE_FIELD" /> ]
		{
			get
			{
				return this.FindByName( p<xsl:value-of select="$TITLE_FIELD" /> );
			}
		}

		public <xsl:value-of select="$v_DetailName" /> FindByName( string p<xsl:value-of select="$TITLE_FIELD" /> )
		{
			<xsl:value-of select="$v_DetailName" /> v_oReturn = null;
			
			foreach ( <xsl:value-of select="$v_DetailName" /> v_oCurrent in this.List )
			{
				if ( v_oCurrent.<xsl:value-of select="$TITLE_FIELD" /> == p<xsl:value-of select="$TITLE_FIELD" /> )
				{
					v_oReturn = v_oCurrent;
					
					break;
				}
			}
			
			return ( v_oReturn );
		}
		</xsl:if>
				
		public virtual void LoadXml( XmlElement p_oSource )
		{
			<xsl:variable name="v_ObjectName" select="@object" />
			<xsl:variable name="v_DetailElementName" select="//lzc:object[ @name = $v_ObjectName ]/@alias" />
			<xsl:value-of select="$v_DetailName" /> v_oNew;
			
			foreach ( XmlElement v_elCurrent in p_oSource.SelectNodes( "<xsl:value-of select="concat( $v_NamespacePrefix, $v_DetailElementName )" />", PersistenceConfig.Namespaces ) )
			{
				v_oNew = new <xsl:value-of select="$v_DetailName" />();
				
				v_oNew.LoadXml( v_elCurrent );

				this.Add( v_oNew );
			}
		}
		
		public virtual XmlElement SaveXml( XmlDocument p_oDocument )
		{
			if ( this.List.Count > 0 )
			{
				XmlElement	v_elNew	= p_oDocument.CreateElement( "<xsl:value-of select="//lzc:project/@namespaceprefix" />", "<xsl:value-of select="@alias" />", "<xsl:value-of select="//lzc:project/@namespaceuri" />" );
				
				foreach ( <xsl:value-of select="$v_DetailName" /> v_oCurrent in this.List )
				{
					v_elNew.AppendChild( v_oCurrent.SaveXml( p_oDocument ) );
				}
				
				return ( v_elNew );
			}
			else
			{
				return ( null );
			}
		}
   }
}
   </xsl:template>

</xsl:stylesheet>